# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import reply
import receive
import web

class Handle(object):
    
    def GET(self):
        try:
            data = web.input()
            if len(data) == 0:
                return "Hello, What are you nong sha lei ? ? ?"
            signature = data.signature
            timestamp = data.timestamp
            nonce = data.nonce
            echostr = data.echostr
            token = "madison"  

            list = [token, timestamp, nonce]
            list.sort()
            sha1 = hashlib.sha1()
            map(sha1.update, list)
            hashcode = sha1.hexdigest()
            print "handle/GET func: hashcode, signature: ", hashcode, signature
            if hashcode == signature:
                return echostr
            else:
                return ""
        except Exception, Argument:
            return Argument

    def POST(self):
        try:
            webData = web.data()
            print "\n\nHandle Post webdata is :\n", webData  #后台打印日志                    
            recMsg = receive.parse_xml(webData) 
            if isinstance(recMsg, receive.Msg):
                toUser = recMsg.FromUserName
                fromUser = recMsg.ToUserName

                if recMsg.MsgType == 'text':                
                    #content = "text"          
                    content = "你刚才说的是：" #+ str(recMsg.content)
                    replyMsg = reply.TextMsg(toUser, fromUser, content)
                    return replyMsg.send()              
                
                if recMsg.MsgType == 'image':
                    mediaId = recMsg.MediaId
                    replyMsg = reply.ImageMsg(toUser, fromUser, mediaId)
                    return replyMsg.send()

                if recMsg.MsgType == 'LOCATION':
                    location_x = recMsg.Location_X
                    location_y = recMsg.Location_Y
                    content = "您所在的位置是在：经度为"+location_x+"；纬度为："+location_y
                    replyMsg = reply.TextMsg(toUser, fromUser, content)
                    return replyMsg.send()

                if recMsg.MsgType == 'event':
                    #print('yes')
                    event = recMsg.Event
                    if event == 'subscribe':
                        content = "欢迎关注.我的树莓PI"
                        replyMsg = reply.TextMsg(toUser, fromUser, content)
                        return replyMsg.send()
                else:
                    return reply.Msg().send()
                    
            if isinstance(recMsg, receive.EventMsg):
                if recMsg.Event == 'CLICK':
                    if recMsg.Eventkey == 'mpGuide':
                        content = u"编写中，尚未完成".encode('utf-8')
                        replyMsg = reply.TextMsg(toUser, fromUser, content)
                        return replyMsg.send()
            else:
                print "other 暂且不处理\n\n"
                return reply.Msg().send()

        except Exception, Argment:
            return Argment